package com.github.controller.cms;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.module.cms.User;
import com.github.pagehelper.PageInfo;
import com.github.service.cms.UserService;

/**
 * <p>User: Mu Yunlong
 * <p>Date: 2016年2月2日 下午7:04:51
 * <p>Version: 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    
    private static final String LIST_PAGE = "uum/user/list";
    private static final String ADD_PAGE = "uum/user/add";
    private static final String EDIT_PAGE = "uum/user/edit";
    private String redirect_list = "redirect:list";
    
    @Resource
    UserService t00_userService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ModelAndView list(User t00_user,
                                @RequestParam(required = false, defaultValue = "1") int page,
                                @RequestParam(required = false, defaultValue = "100") int rows) {
        ModelAndView result = new ModelAndView(LIST_PAGE);
        //PageHelper.startPage(page, rows);
        List<User> countryList = t00_userService.getUserList(t00_user);
        result.addObject("pageInfo", new PageInfo<User>(countryList));
        result.addObject("queryParam", t00_user);
        result.addObject("page", page);
        result.addObject("rows", rows);
        return result;
    }
    
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addPage(User t00_user) {
        ModelAndView result = new ModelAndView(ADD_PAGE);
        result.addObject("t00_user", t00_user);
        return result;
    }
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView add(User t00_user) {
        ModelAndView result = new ModelAndView(redirect_list);
        t00_userService.insertUser(t00_user);
        result.addObject("t00_user", t00_user);
        return result;
    }
    @RequestMapping(value = "/edit_{userid}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable Long userid) {
        ModelAndView result = new ModelAndView(EDIT_PAGE);
        User t00_user = new User();
        t00_user.setUserid(userid);
        t00_user = t00_userService.getT00_userDisp(t00_user);
        result.addObject("t00_user", t00_user);
        return result;
    }
    
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ModelAndView edit(User t00_user) {
        ModelAndView result = new ModelAndView(redirect_list);
        t00_userService.updateUser(t00_user);
        result.addObject("t00_user", t00_user);
        return result;
    }
    
    @RequestMapping(value = "/delete_{userid}", method = RequestMethod.GET)
    public ModelAndView deleteDo(@PathVariable Long userid) {
        ModelAndView result = new ModelAndView(redirect_list);
        t00_userService.deleteT00_user(userid);
        return result;
    }
    
    
    
}
